package com.app.utils;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;

import com.app.config.WeiboConfig;
import com.app.dao.mysql.YjDataMapper;
import com.app.domain.jwjb.YjDataTime;

import com.app.service.YjDataService;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.junit.Test;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Test1 {
    @Autowired
    YjDataMapper yjDataMapper;
    @Autowired
    YjDataService yjDataService;
    @Resource
    private WeiboConfig weiboConfig;
    @Test
    public void test() {
        yjDataMapper.ConditionUpdate("38", "mudjack_pressure", "12");
    }

    @Test
    public void test1() {
//        Random random = new Random();
        float Max = 89.2f, Min = 88.0f;
        for (int i = 0; i < 10; i++) {
            BigDecimal db = new BigDecimal(Math.random() * (Max - Min) + Min);
            System.out.println(db.setScale(1, BigDecimal.ROUND_HALF_UP)// 保留30位小数并四舍五入

                    .toString());
        }
        for (int i = 0; i < 10; i++) {
            double randomDouble = MathUtil.getRandomDouble(88.0, 89.2);
            System.out.println(randomDouble);
        }

    }

    @Test
    public void test2() {
        List<Double> list = new ArrayList<>();
        Random rand = new Random();
        double yjMAX = 0.3;
        double yjMIN = 0.5;
        double yjResult = 0;
        list.add(0.0);
        for (int i = 0; i < 20; i++) {
            yjResult = yjMIN + (rand.nextDouble() * (yjMAX - yjMIN));
            yjResult = (double) Math.round(yjResult * 100) / 100;
            list.add(yjResult);
        }
        double byMAX = 0.5;
        double byMIN = 0.7;
        double byResult = 0;
        for (int i = 0; i < 12; i++) {
            byResult = byMIN + (rand.nextDouble() * (byMAX - byMIN));
            byResult = (double) Math.round(byResult * 100) / 100;
            list.add(byResult);
        }
        Collections.sort(list);
        System.out.println(list);
        System.out.println("count:" + list.size());
    }

    @Test
    public void fe_map() throws Exception {
        TemplateExportParams params = new TemplateExportParams(
                "d://jwjbTemplate.xls");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("lh", "1");
        map.put("yjrq", "2");
        map.put("phb", "3");
        map.put("hjwd", "4");
        map.put("jywd", "5");
        map.put("ldd", "6");

        List<Map<String, String>> listMap = new ArrayList<Map<String, String>>();
        for (int i = 0; i < 4; i++) {
            Map<String, String> lm = new HashMap<String, String>();
            lm.put("duct_code", "222");
            lm.put("yjfx", "333");
            lm.put("yjsj", "09:03:04");
            lm.put("ylz", "50");
            lm.put("zkd", "");
            lm.put("yjl", "");
            lm.put("wysj", "正常");

            lm.put("status", "正常");

            listMap.add(lm);
        }
        map.put("maplist", listMap);

        Workbook workbook = ExcelExportUtil.exportExcel(params, map);
        File savefile = new File("D:/excel/");
        if (!savefile.exists()) {
            savefile.mkdirs();
        }
        FileOutputStream fos = new FileOutputStream("D:/excel/鸡尾酒表.xlsx");
        workbook.write(fos);

        fos.close();

    }


    public void writeInTemplate(XSSFSheet spreadsheet, String newContent, int beginRow, int beginCell) {
        XSSFRow row = spreadsheet.getRow(beginRow);
        if (null == row) {
            //如果不做空判断，你必须让你的模板文件画好边框，beginRow和beginCell必须在边框最大值以内
            //否则会出现空指针异常
            row = spreadsheet.createRow(beginRow);
        }
        XSSFCell cell = row.getCell(beginCell);
        if (null == cell) {
            cell = row.createCell(beginCell);
        }
        //设置存入内容为字符串
//        cell.setCellType(HSSFCell.CELL_TYPE_STRING);
        //向单元格中放入值
        cell.setCellValue(newContent);

    }

    @Test
    public void test05() {
        for (int i = 0; i < 20; i++) {
            double randomDouble = MathUtil.getRandomDouble(0.5, 0.7);
            System.out.println(randomDouble);
        }
    }

    @Test
    public void test07() throws ParseException {
        List<String> list1 = new ArrayList<>();
        List<String> list2 = new ArrayList<>();
        YjDataTime yjDataTime = yjDataMapper.findStartAndEndTimeById("71");
        String mcgById = yjDataMapper.findMcgById("71");
        Long subTime = TimeHelper.subTime(yjDataTime.getStartTime(), yjDataTime.getEndTime());
        int ii = (int) (subTime / 5);
        System.out.println(subTime);
        System.out.println(ii);
        String[] split = mcgById.split("-");
        if (ii > split.length) {
            for (int i = 0; i < ii; i++) {
                double randomDouble = MathUtil.getRandomDouble(0.5, 0.7);
                list2.add(Double.toString(randomDouble));
            }
        } else {
            String[] newData;
            newData = Arrays.copyOfRange(split, split.length - ii - 1, split.length);
            list2 = Arrays.asList(newData);
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = null;
        try {
            parse = df.parse(yjDataTime.getStartTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String format;
        list1.add(yjDataTime.getStartTime().split(" ")[1]);
        for (int i = 0; i < ii; i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(parse);
            calendar.add(Calendar.SECOND, +5);
            parse = calendar.getTime();
            format = df.format(calendar.getTime());
            list1.add(format.split(" ")[1]);
        }
        System.out.println(list1);
        System.out.println(list1.size());
        System.out.println(list2);
        System.out.println(list2.size());
    }

    @Test
    public void test11() {
        Random random=new Random();

        for (int j = 0; j < 10; j++) {
            int i = random.nextInt(10)+300 ;
            System.out.println(i);
        }
    }

    @Test
    public void test12() {
/*        Map<String, List> graphData = yjDataService.getGraphData(String.valueOf(6319));
        List<String> dateList = graphData.get("dateList");
        List<String> mcgList = graphData.get("mcgList");

        System.out.println(dateList);*/
        String projectId = weiboConfig.getProjectId();
        System.out.println("projectId:"+projectId);
    }
}
